import { Request, Response } from 'express';

// 模拟角色数据
const roles: any[] = [
  { id: 1, name: '超级管理员', description: '拥有所有权限' },
  { id: 2, name: '普通管理员', description: '部分权限' },
  { id: 3, name: '访客', description: '只读权限' },
];

// 模拟分页数据
const getPageData = (params: any) => {
  const { current = 1, pageSize = 10 } = params;
  const start = (current - 1) * pageSize;
  const end = start + Number(pageSize);
  return {
    content: roles.slice(start, end),
    totalElements: roles.length,
    current: Number(current),
    pageSize: Number(pageSize),
  };
};

export default {
  // 模拟获取角色列表接口
  'GET /api/system/role': (req: Request, res: Response) => {
    const pageData = getPageData(req.query);
    res.status(200).json({
      code: 200,
      message: '获取角色列表成功',
      data: pageData,
    });
  },

  // 模拟添加角色接口
  'POST /api/system/role': (req: Request, res: Response) => {
    const newRole = req.body;
    newRole.id = roles.length + 1;
    roles.push(newRole);
    res.status(200).json({
      code: 200,
      message: '添加角色成功',
      data: newRole,
    });
  },

  // 模拟更新角色接口
  'PUT /api/system/role': (req: Request, res: Response) => {
    const updatedRole = req.body;
    const index = roles.findIndex((role) => role.id === updatedRole.id);
    if (index !== -1) {
      roles[index] = updatedRole;
      res.status(200).json({
        code: 200,
        message: '更新角色成功',
        data: updatedRole,
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '角色不存在',
      });
    }
  },

  // 模拟删除角色接口
  'DELETE /api/system/role/:id': (req: Request, res: Response) => {
    const id = parseInt(req.params.id, 10);
    const index = roles.findIndex((role) => role.id === id);
    if (index !== -1) {
      roles.splice(index, 1);
      res.status(200).json({
        code: 200,
        message: '删除角色成功',
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '角色不存在',
      });
    }
  },

  // 模拟根据角色 ID 获取单个角色详细信息接口
  'GET /api/system/role/:id': (req: Request, res: Response) => {
    const id = parseInt(req.params.id, 10);
    const role = roles.find((role) => role.id === id);
    if (role) {
      res.status(200).json({
        code: 200,
        message: '获取角色详情成功',
        data: role,
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '角色不存在',
      });
    }
  },

  // 模拟获取角色树形结构接口
  'GET /api/system/role/tree': (req: Request, res: Response) => {
    // 这里简单返回全部角色作为树形结构，实际可按需调整
    res.status(200).json({
      code: 200,
      message: '获取角色树形结构成功',
      data: roles,
    });
  },

  // 模拟获取角色选项接口
  'GET /api/system/role/options': (req: Request, res: Response) => {
    const options = roles.map((role) => ({ id: role.id, name: role.name }));
    res.status(200).json({
      code: 200,
      message: '获取角色选项成功',
      data: options,
    });
  },
};
